package cn.itcast.service.impl;


import cn.itcast.dao.MemberDao;
import cn.itcast.dao.ReportDao;
import cn.itcast.service.ReportService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private MemberDao memberDao;

    /**
     * 会员数量统计
     *
     * @param months
     * @return
     */
    @Override
    public List<Integer> findMemberByMonths ( List<String> months ) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";
            Integer count = this.memberDao.findMemberByMonths(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    /**
     * 运营数据统计
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> findBusiness () throws Exception {
        Map<String, Object> map = new HashMap<>();
        //当前日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        map.put("reportDate", today);
        //获取本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月一的日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //本日新增会员数
        Integer todayNewMember = this.memberDao.findMemberByDate(today);
        //总会员数
        Integer totalMember = this.memberDao.findMemberTotal();
        //本周新增会员数eek
        Integer thisWeekNewMember = this.memberDao.findMemberCountWeekAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = this.memberDao.findMemberByMonths(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = this.reportDao.findOrderCountByDate(today);
        //今日到诊数
        Integer todayVisitsNumber = this.reportDao.findVisitsCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = this.reportDao.findOrderCountWeekAfterDate(thisWeekMonday);
        //本周到诊数
        Integer thisWeekVisitsNumber = this.reportDao.findVisitsCountWeekAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = this.reportDao.findOrderCountMonthAfterDate(firstDay4ThisMonth);
        //本月到诊数
        Integer thisMonthVisitsNumber = this.reportDao.findVisitsCountMonthAfterDate(firstDay4ThisMonth);


        //热门套餐。预约数据多的 order.setmeal
        List<Map> hotSetmeal = this.reportDao.findhotSetmeal();
        map.put("todayNewMember", todayNewMember);
        map.put("totalMember", totalMember);
        map.put("thisWeekNewMember", thisWeekNewMember);
        map.put("thisMonthNewMember", thisMonthNewMember);
        map.put("todayOrderNumber", todayOrderNumber);
        map.put("todayVisitsNumber", todayVisitsNumber);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        map.put("hotSetmeal", hotSetmeal);
        return map;
    }

    @Override
    public List<Double> findIncome ( List<String> months ) {
        List<Double> memberCount = new ArrayList<>();
        //对months进行处理
        for (String month : months) {
            String date = month + ".31";
            Double count = this.reportDao.findIncomeByMonths(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    @Override
    public List<Integer> findOrder ( List<String> months ) {
        List<Integer> orderCount = new ArrayList<>();
        //对months进行处理
        for (String month : months) {
            String date = month + ".31";
            Integer count = this.reportDao.findOrderByMonths(date);
            orderCount.add(count);
        }
        return orderCount;
    }


}